﻿declare var timeago: () => any;
$(function () {
    $("#btnQuery").click(function () {
        //$.ajax("getGlFiles", { type: "POST", dataType: 'json',
        //    data: {
        //        sslb: $("#selSSLB").val(),
        //        sszy: $("#selSSZY").val(),
        //        ssyt: $("#selSSYT").val(),
        //        sbr: $("#txtSCR").val(),
        //        scsj: $("#selSCSJ").val()
        //    }
        //}).done(function (files) {
        //返回的files包含以下列：
        //FILENAME,SCSJ,SLLB,SLZY,SSYT,SCR
        var files = [{ ID: 0, FILENAME: '文件名1', SCSJ: '2016-01-10 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 1, FILENAME: '文件名2', SCSJ: '2016-10-11 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 2, FILENAME: '文件名3', SCSJ: '2016-08-12 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 3, FILENAME: '文件名4', SCSJ: '2016-10-13 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 4, FILENAME: '文件名5', SCSJ: '2016-09-14 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 0, FILENAME: '文件名1', SCSJ: '2016-01-10 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 1, FILENAME: '文件名2', SCSJ: '2016-10-11 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 2, FILENAME: '文件名3', SCSJ: '2016-08-12 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 3, FILENAME: '文件名4', SCSJ: '2016-10-13 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 4, FILENAME: '文件名5', SCSJ: '2016-09-14 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 0, FILENAME: '文件名1', SCSJ: '2016-01-10 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 1, FILENAME: '文件名2', SCSJ: '2016-10-11 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 2, FILENAME: '文件名3', SCSJ: '2016-08-12 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 3, FILENAME: '文件名4', SCSJ: '2016-10-13 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' },
            { ID: 4, FILENAME: '文件名5', SCSJ: '2016-09-14 00:00:00', SLLB: '会议', SLZY: '天然气', SSYT: '胜利', SCR: '申君健' }]
        renderTableview(files);
        //});
    });
    //输出为表格形式
    var timeAgo = timeago();
    function renderTableview(files: any[]) {
        $("#tblFile").autoTable({
            data: files,
            header: [[{ title: '#', span: [1, 1] }, { title: '文件名|FILENAME', span: [1, 1] }, { title: '上传时间|SCSJ', span: [1, 1] },
                { title: '上传人|SCR', span: [1, 1] }, { title: '所属类别|SLLB', span: [1, 1] },
                { title: '所属专业|SLZY', span: [1, 1] }, { title: '所属油田|SSYT', span: [1, 1] },
                { title: '操作|CZ', span: [1, 1] }]],
            headerFields: ["FILENAME", "SCSJ", "SCR", "SLLB", "SLZY", "SSYT", "CZ"],
            sortFields: ["FILENAME", "SCSJ", "SCR", "SLLB", "SLZY", "SSYT"],
            keydataOnRow: ["ID"],
            columns: {
                CZ: { formator: function (v, r) { return `<a href="#" >删除</a>`; } },
                SCSJ: {
                    formator: function (v: string, r) {
                        return timeAgo.format(v, 'zh_CN');// v.substr(0, 16);
                    }
                }
            },
            isScrollable: true,
            freezeContainer: '#tableview',
            // freezeFields:['FILENAME'],
            isRownumber: true,
            isPageable: files.length > 50,
            selectMode: AUTO.SelectMode.SINGLE
        });
    };
    //删除文件
    $("#tblFile").on("click", '.tblFile_CZ a', function () {
        var $tr = $(this).parents("tr");
        var id = $tr.data("id");
        var filename = $tr.find(".tblFile_FILENAME").text();
        $.auto.confirm("提示", "确定要删除 " + filename + " 吗?", function () {
            console.log("已经删除" + filename);
            //移除行，但序号会空。要不重新输出一下表格也可以
            $tr.remove();
        });
        return false;
    });

    //锁定表头
    $(window).resize(function () {
        $("#tableview").css({
            height: ($(window).innerHeight() - $("#tableview").offset().top - 10) + 'px'
        });
    }).resize();
});